package com.hyb.algorithm.data.struct.dp;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

/**
 * @Author: huyanbing
 * @Date: 2021/8/12 4:51 下午
 */
public class OpenLock {

    public static void main(String[] args) {

        String[] deadends = new String[]{"0201", "0101", "0102", "1212", "2002"};
        String target = "0202";

        OpenLock openLock = new OpenLock();
        System.out.println(openLock.openLock(deadends, target));

    }


    public int openLock(String[] deadends, String target) {

        return openBsf(deadends, target);
    }

    /**
     * 将 s[j] 向上拨动一次
     *
     * @param s
     * @param i
     * @return
     */
    public String plusOne(String s, int i) {

        char[] ch = s.toCharArray();

        if (ch[i] == '9') {
            ch[i] = '0';
        } else {
            ch[i] += 1;
        }

        return new String(ch);
    }


    /**
     * 将 s[i] 向下拨动一次
     *
     * @param s
     * @param i
     * @return
     */
    public String minusOne(String s, int i) {
        char[] ch = s.toCharArray();

        if (ch[i] == '0') {
            ch[i] = '9';
        } else {
            ch[i] -= 1;
        }

        return new String(ch);
    }


    /**
     * 输入：deadends = ["0201","0101","0102","1212","2002"], target = "0202"
     * 输出：6
     * 解释：
     * 可能的移动序列为 "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202"。
     * 注意 "0000" -> "0001" -> "0002" -> "0102" -> "0202" 这样的序列是不能解锁的，
     * 因为当拨动到 "0102" 时这个锁就会被锁定。
     */
    public int openBsf(String[] deadends, String target) {

        Queue<String> queue = new LinkedList<>();

        Set<String> deadEndMap = new HashSet<>();

        // 记录已经穷举过的密码，防⽌⾛回头路
        Set<String> visited = new HashSet<>();

        for (String deadStr : deadends) {
            deadEndMap.add(deadStr);
        }

        queue.offer("0000");
        visited.add("0000");

        int step = 0;

        while (!queue.isEmpty()) {

            int levelNum = queue.size();

            //将当前节点向周围扩散
            for (int i = 0; i < levelNum; i++) {

                String curr = queue.poll();
                visited.add(curr);

                /* 判断是否到达终点 */
                if (deadEndMap.contains(curr)) {
                    continue;
                }

                /* 判断是否到达终点 */
                if (curr.equals(target)) {
                    return step;
                }

                int targetSize = target.length();
                /* 将⼀个节点的未遍历相邻节点加⼊队列 */
                for (int j = 0; j < targetSize; j++) {
                    String plus = plusOne(curr, j);
                    String minus = minusOne(curr, j);

                    if (!visited.contains(plus)) {
                        queue.offer(plus);
                        visited.add(plus);
                    }


                    if (!visited.contains(minus)) {
                        queue.offer(minus);
                        visited.add(minus);
                    }
                }
            }

            //这里增加步数
            step++;
        }


        // 如果穷举完都没找到⽬标密码，那就是找不到了
        return -1;
    }


}
